/*
 * TimersPool.java
 *
 * Created on 26 de marzo de 2006, 05:48 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package inc.jmc.cyberboard.ui.print;

import inc.jmc.cyberboard.ui.print.vo.ImpresionJobBean;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import org.apache.log4j.Logger;

/**
 *
 * @author Juan Miguel Olguin Salguero
 */
public class PrinterReportClock extends TimerTask implements java.io.Serializable{
    /**
	 * 
	 */
	private static final long serialVersionUID = 9165879933675519025L;
	static Logger logger = Logger.getLogger(PrinterReportClock.class.getName());
    public static String CURR_DIRECTORY="";
    public HashMap<Long,ImpresionJobBean> jobsGlobalList=new HashMap<Long,ImpresionJobBean>();
    static {
    	File f=new File("");
    	CURR_DIRECTORY=f.getAbsolutePath();
    	System.out.println("Esta es la ruta en la que esta corriendo el cliente:"+CURR_DIRECTORY);
    	
    }
    @SuppressWarnings("unused")
	private PrinterReportClock()
    {
    }
    public void actualizaJobsList(Vector<ImpresionJobBean> lista)
    {
    	//ImpresionJobBean current=null;
    	
    	for (Iterator<ImpresionJobBean> iter = lista.iterator(); iter.hasNext();) {
    		ImpresionJobBean element = iter.next();
    		Long idDeTrabajo = new Long(element.getIdDeTrabajo());
    		ImpresionJobBean object = jobsGlobalList.get(idDeTrabajo);
    		if(object==null)
    		{
    			jobsGlobalList.put(idDeTrabajo, element);
    			
    		}
    		else
    		{
    			int num1 = element.getNumeroTotalDePaginas();
    			int num2 = object.getNumeroTotalDePaginas();
    			if(num1>num2)
    			{
    				jobsGlobalList.put(idDeTrabajo, element);
    				logger.debug(element);
    			}
    		}
    		logger.info("////////////////");
    		logger.info("////////////////");
    		logger.info("este la lista de jobs--->"+jobsGlobalList);
    		logger.info("////////////////");
    		logger.info("////////////////");
    		
			
		}
    	//return null;
    }
    public void run()
    {
    	boolean runit=true;
    	if(runit)
    	{
    		ParserPrinterJobs parser=new ParserPrinterJobs (); 
        	try {
    			InputStream inputStream = Runtime.getRuntime().exec(CURR_DIRECTORY+"\\opciones\\cscript.exe "+CURR_DIRECTORY+"\\opciones\\prnjobs.vbs -l").getInputStream();
    			Vector<ImpresionJobBean> reports = parser.getReports(inputStream);
    			if(reports.size()>0)
    			{
    				
    				logger.debug(" esto es lo que hay en el sistema");
    				logger.debug(reports);
    				this.actualizaJobsList(reports);
    			}
    			
    			
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    	
        
    }
    /** Creates a new instance of TimersPool */
    public PrinterReportClock(float seccs)
    {
        seccs*=1000;
        Timer timer = new Timer();
        timer.schedule(this, 0L, (int)seccs);
    }
    public static void main(String args[])throws java.io.IOException
    {
        //PrinterReportClock pct=new PrinterReportClock(1);
    }
    
    
}

